<?php
/**
 * 清理PHP缓存和ThinkPHP缓存
 */

echo "========================================\n";
echo "清理缓存\n";
echo "========================================\n\n";

// 1. 清理OPcache
if (function_exists('opcache_reset')) {
    echo "🔄 清理OPcache...\n";
    opcache_reset();
    echo "✅ OPcache已清理\n\n";
} else {
    echo "⚠️  OPcache未启用\n\n";
}

// 2. 清理ThinkPHP缓存
$runtimePath = __DIR__ . '/runtime';
if (is_dir($runtimePath)) {
    echo "🔄 清理ThinkPHP运行时缓存...\n";
    
    $files = new RecursiveIteratorIterator(
        new RecursiveDirectoryIterator($runtimePath, RecursiveDirectoryIterator::SKIP_DOTS),
        RecursiveIteratorIterator::CHILD_FIRST
    );
    
    $count = 0;
    foreach ($files as $fileinfo) {
        if ($fileinfo->isDir()) {
            @rmdir($fileinfo->getRealPath());
        } else {
            @unlink($fileinfo->getRealPath());
            $count++;
        }
    }
    
    echo "✅ 已清理 {$count} 个缓存文件\n\n";
} else {
    echo "⚠️  运行时目录不存在\n\n";
}

// 3. 检查文件语法
echo "🔍 检查PHP文件语法...\n";

$files = [
    'application/service/MessageParser/Vendor1MessageParser.php',
    'application/service/MessageParser/Vendor2MessageParser.php',
    'application/service/MessageParser/MessageParserFactory.php',
    'application/service/MessageParser/MessageParserInterface.php',
];

$errors = [];
foreach ($files as $file) {
    $fullPath = __DIR__ . '/' . $file;
    if (file_exists($fullPath)) {
        $output = [];
        $returnCode = 0;
        exec("php -l " . escapeshellarg($fullPath), $output, $returnCode);
        
        if ($returnCode === 0) {
            echo "✅ {$file}\n";
        } else {
            echo "❌ {$file}\n";
            $errors[] = $file;
            foreach ($output as $line) {
                echo "   {$line}\n";
            }
        }
    } else {
        echo "⚠️  {$file} 不存在\n";
    }
}

echo "\n";
if (empty($errors)) {
    echo "🎉 所有文件语法检查通过！\n";
} else {
    echo "❌ 发现 " . count($errors) . " 个文件有语法错误\n";
}

echo "\n========================================\n";
echo "缓存清理完成\n";
echo "========================================\n";

